<?php

namespace app\controller;

use app\service\CamperService;
use support\Request;
use Throwable;
use const support\ERROR;
use const support\OPERATE_ERROR;

class CamperController
{

    //营员列表
    public function index(Request $request): \support\Response
    {
        try {
            $lists=CamperService::lists(getMemberId());
            return buildSuccess($lists);
        }catch (Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //营员详情
    public function details(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'id'	=>'',
        ]);
        try {
            $info=CamperService::details(getMemberId(),$params['id']);
            if (!$info){
                return buildFail(OPERATE_ERROR,'无效数据');
            }
            return buildSuccess($info);
        }catch (Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //营员新增
    public function add(Request $request): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'real_name'	    =>'',
            'card_number'	=>'',
            'member_icon'   =>'',
            'member_height' =>'',
            'member_weight' =>'',
            'member_type'   =>'',
            'guardian_name' =>'',
            'guardian_mobile'=>'',
            'urgent_contact_name'=>'',
            'urgent_contact_mobile'=>'',
            'remarks'=>''
        ]);
        try {
            $ret=CamperService::toAdd(getMemberId(),$params);
            if (!$ret){
                return buildFail(OPERATE_ERROR,'添加失败');
            }
            return buildSuccess();
        }catch (Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //营员更新
    public function edit(Request $request): \support\Response
    {
        try {
            //接收前端传参
            $params=getRequestData([
                'id'            =>'',
                'real_name'	    =>'',
                'card_number'	=>'',
                'member_icon'   =>'',
                'member_height' =>'',
                'member_weight' =>'',
                'member_type'   =>'',
                'guardian_name' =>'',
                'guardian_mobile'=>'',
                'urgent_contact_name'=>'',
                'urgent_contact_mobile'=>'',
                'remarks'=>''
            ]);
            $ret=CamperService::toEdit(getMemberId(),$params['id'],$params);
            if (!$ret){
                return buildFail(OPERATE_ERROR,'更新失败');
            }
            return buildSuccess();
        }catch (Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //营员删除
    public function del(Request $request): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'id'	=>'',
        ]);
        try {
            $ret=CamperService::toDel(getMemberId(),$params['id']);
            if (!$ret){
                return buildFail(OPERATE_ERROR,'删除失败');
            }
            return buildSuccess();
        }catch (Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

}